<?php
namespace Admin\Controller;

class CustomerController extends CommonController{
	
	public function index(){
		$this->where = array();
        $this->where['type'] = '2';
        $this->where['delete_status'] = '1';
        I('get.keywords') && $this->where['nickname|username'] = array('like','%'.I('get.keywords').'%');
        $this->order = 'add_time desc';
        parent::index();
	}
    public function add(){
		$currentModel = D(CONTROLLER_NAME);
        if(IS_POST){
            $data = I('post.info');
            if(!$m=$currentModel->create($data)) $this->error($currentModel->getError());
            $data['password'] &&$data['password'] = md5($data['password']);
            $res = $currentModel->saveData($data);
            $res === false ? $this->error('保存失败') : $this->success('保存成功',U('index'));
        }else{
            $formData = array();
            I('get.id') && $formData = D(CONTROLLER_NAME)->find((int)I('get.id'));
            $this->assign('formData',$formData);
            $this->display();
        }
    }
    /**
     * 删除商户
     */
    public function delete_data(){
    	$result = D(CONTROLLER_NAME)->where(array('id'=>intval(I('post.id'))))->save(array('delete_status'=>'2'));
    	$result?$this->success('删除成功'):$this->error('删除失败');
    }
    //修改状濿
    public function set_status($id, $status = '2'){
        D(CONTROLLER_NAME)->where(array('id'=>$id))->save(array('status'=>$status));
    }
    
    //修改密码
    public function editp(){
        if(IS_POST){
	        empty(I('post.id')) && $this->error('修改失败');
	        empty(I('post.password')) && $this->error('请输入新密码');
	        D(CONTROLLER_NAME)->create(I('post.')) || $this->error(D(CONTROLLER_NAME)->getError());
	        $res = D(CONTROLLER_NAME)->where(array('id'=>I('post.id')))->save(array('password'=>md5(I('post.password'))));
	        $res === false ? $this->error('修改失败') : $this->success('修改成功');
        }else{
        	$this->display();
        }
    }
    
    public function set_power(){
    	if(IS_POST){
    		D(CONTROLLER_NAME)->create(I('post.')) || $this->error(D(CONTROLLER_NAME)->getError());
    		$res = D(CONTROLLER_NAME)->saveData(I('post.'));
    		$res === false ? $this->error('修改失败') : $this->success('修改成功');
    	}else{
    		$my_power = '';
    		I('get.id') && $my_power = D(CONTROLLER_NAME)->where(array('id'=>(int)I('get.id')))->getField('power');
    		$this->assign('my_power',output_data($my_power));
    		$this->assign('powers',C('POWER'));
    		$this->display();
    	}
    }
    
    public function edit_pass(){
    	if(IS_POST){
    		$curModel = D('Customer');
    		$data = I('post.info');
    		if (false === $curModel->create($data)) $this->error($curModel->getError());
    
    		$old_pass = md5($data['old_password']);
    		$user_info = $curModel->where("username='{$this->customer['username']}' and password='$old_pass' and dele_status='1'")->find();
    		if(empty($user_info))$this->error('原密码错误！');
    
    		$nickname = trim($data['my_nickname']);
    		$new_pass = md5($data['new_password']);
    
    		$res = $curModel->save(array('id'=>$this->customer['id'],'nickname'=>$nickname,'password'=>$new_pass));
    		$res ? $this->success("修改成功！") : $this->error("修改失败！");
    	}else{
    		$this->display();
    	}
    }
    
}
